package com.pipi.sso.core.model;

import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.dao.support.PageInfo;
import lombok.Data;

import java.util.Map;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.pipi.sso.core.model
 * @date: 2019/3/30 1:43 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 */
@Data
public class BizResult<T> {
    private boolean success = false;
    private String code;
    private String message;
    private T data;
    private Map<String, Object> extraInfo;
    private int pageNo;
    private int totalCount;
    private int pageSize;

    public static <T> BizResult<T> success(T data) {
        return create(data);
    }

    public static <T> BizResult<T> empty() {
        BizResult<T> result = create();
        result.setSuccess(true);
        return result;
    }

    public static <T> BizResult<T> error(String code, String message) {
        return create(code, message);
    }

    public static <T, S> BizResult<T> error(BizResult<S> errorBizResult) {
        return error(errorBizResult.getCode(), errorBizResult.getMessage());
    }


    public static <T> BizResult<T> systemError(String msg) {
        return error(DormErrorCodeEnum.EXCEPTION.getErrCode(), msg);
    }

    public BizResult() {
        this.pageNo = BaseQuery.DEFAULT_PAGE;
        this.pageSize = BaseQuery.DEFAULT_PAGE_SIZE;
    }

    public static <T> BizResult<T> create() {
        return new BizResult();
    }

    public static <T> BizResult<T> create(T data) {
        BizResult<T> bizResult = create();
        bizResult.setSuccess(true);
        bizResult.setData(data);
        return bizResult;
    }

    public static <T> BizResult<T> create(T data, PageInfo pageInfo) {
        BizResult<T> bizResult = BizResult.create();
        bizResult.setSuccess(true);
        bizResult.setData(data);
        bizResult.setPageNo(pageInfo.getPageNo());
        bizResult.setPageSize(pageInfo.getPageSize());
        bizResult.setTotalCount(pageInfo.getTotalCount());
        bizResult.setCode("0");
        return bizResult;
    }

    public static <T> BizResult<T> create(T data, String code, String message) {
        BizResult<T> result = create();
        result.setSuccess(true);
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> BizResult<T> create(T data, boolean isSuccess, String code, String message) {
        BizResult<T> result = create();
        result.setSuccess(isSuccess);
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> BizResult<T> create(T data, Map<String, Object> extraInfo) {
        BizResult<T> bizResult = create();
        bizResult.setSuccess(true);
        bizResult.setData(data);
        bizResult.setExtraInfo(extraInfo);
        return bizResult;
    }

    public static <T> BizResult<T> create(T data, Map<String, Object> extraInfo, boolean isSuccess, String code, String message) {
        BizResult<T> bizResult = create();
        bizResult.setSuccess(true);
        bizResult.setData(data);
        bizResult.setExtraInfo(extraInfo);
        bizResult.setSuccess(isSuccess);
        bizResult.setCode(code);
        bizResult.setMessage(message);
        return bizResult;
    }

    public static <T> BizResult<T> create(String code, String message) {
        BizResult<T> bizResult = create();
        bizResult.setSuccess(false);
        bizResult.setCode(code);
        bizResult.setMessage(message);
        return bizResult;
    }

    public static <T> BizResult<T> create(DormErrorCodeEnum errorCodeEnum) {
        BizResult<T> bizResult = create();
        bizResult.setSuccess(false);
        bizResult.setCode(errorCodeEnum.getErrCode());
        bizResult.setMessage(errorCodeEnum.getErrMsg());
        return bizResult;
    }


}
